package com.ruoyi.project.cms.notice.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.notice.domain.CmsNotice;
import com.ruoyi.project.cms.notice.domain.NoticeType;
import com.ruoyi.project.cms.notice.service.CmsNoticeService;
import com.ruoyi.project.cms.notice.service.NoticeTypeService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
/**
 * com.ruoyi.project.cms.notice.controller
 * 作者：lh
 * 描述:
 * 时间:2020-05-18
 */
@Controller
@RequestMapping("/notice/view")
public class CmsNoticeController extends BaseController {

    private String prefix = "cms/notice";

    @Autowired
    private CmsNoticeService cmsNoticeService;

    @Autowired
    private NoticeTypeService typeService;

    @GetMapping("")
    public String type()
    {
        return prefix + "/notice";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CmsNotice cmsNotice)
    {
        startPage();
        //获取当前用户
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        String nname=user.getUserName();
        //获得公告
        List<CmsNotice> list = cmsNoticeService.selectNoticeList(cmsNotice);
        for(CmsNotice c:list){
            //获得范文人员
            String people=c.getNpeople();
            if (people!=null&&!people.equals(nname)){
                people =nname+","+people;
            }else {
                people=nname;
            }
            c.setNpeople(people);
            int num=c.getNpageviews();
            num++;
            c.setNpageviews(num);
            //c获得的是tname，执行修改操作若不加入tid为空
            String tname=c.getTname();
            NoticeType type=typeService.selectByName(tname);
            c.setTid(type.getTid());
            cmsNoticeService.updateByPrimaryKey(c);
        }
        return getDataTable(list);
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 获取所有公告类型
     */
    @PostMapping("/getList")
    public List<NoticeType> getList(){
        //获取所有公告类型
        return typeService.selectAll();
    }

    /**
     * 新增保存岗位
     */
    @Log(title = "公告类型管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated CmsNotice cmsNotice)
    {
        if (UserConstants.POST_NAME_NOT_UNIQUE.equals(cmsNoticeService.checkNoticeNameUnique(cmsNotice)))
        {
            return error("新增公告'" + cmsNotice.getNname() + "'失败，公告已存在");
        }
        //比较data是否合法
        Date date=new Date();
        if (date.compareTo(cmsNotice.getNbtime())!=-1){
            return error("开始实施日期应该不早于当前时间");
        }
        if (cmsNotice.getNbtime().compareTo(cmsNotice.getNotime())!=-1){
            return error("开始实施日期应该在结束日期之后");
        }
        User user= (User) SecurityUtils.getSubject().getPrincipal();
        String nname=user.getUserName();
        cmsNotice.setNanthor(nname);
        cmsNotice.setNtime(date);
        cmsNotice.setNpageviews(1);
        cmsNotice.setNpeople(nname);
        return toAjax(cmsNoticeService.insert(cmsNotice));
    }

    /**
     * 修改公告类型
     */
    @GetMapping("/edit/{nid}")
    public String edit(@PathVariable("nid") Integer nid, ModelMap mmap)
    {

        mmap.put("cmsNotice",cmsNoticeService.selectByPrimaryKey(nid));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @Log(title = "公告类型管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated CmsNotice cmsNotice)
    {
        Date date=new Date();
        if (date.compareTo(cmsNotice.getNbtime())!=-1){
            return error("开始实施日期应该不早于当前时间");
        }
        if (cmsNotice.getNbtime().compareTo(cmsNotice.getNotime())!=-1){
            return error("开始实施日期应该在结束日期之后");
        }
        return toAjax(cmsNoticeService.updateByPrimaryKey(cmsNotice));
    }

    /**
     * 删除公告类型
     * @param ids
     * @return
     */
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(cmsNoticeService.deleteNoticeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 公告类型名校验
     * @param cmsNotice
     * @return
     */
    @PostMapping("/checkNoticeNameUnique")
    @ResponseBody
    public String checkNoticeNameUnique(CmsNotice cmsNotice)
    {
        return cmsNoticeService.checkNoticeNameUnique(cmsNotice);
    }
}
